{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Data Wrangling with Spark\n",
    "\n",
    "This is the code used in the previous screencast. Run each code cell to understand what the code does and how it works.\n",
    "\n",
    "These first three cells import libraries, instantiate a SparkSession, and then read in the data set"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyspark.sql import SparkSession\n",
    "from pyspark.sql.functions import udf\n",
    "from pyspark.sql.types import StringType\n",
    "from pyspark.sql.types import IntegerType\n",
    "from pyspark.sql.functions import desc\n",
    "from pyspark.sql.functions import asc\n",
    "from pyspark.sql.functions import sum as Fsum\n",
    "\n",
    "import datetime\n",
    "\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "%matplotlib inline\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Creating spark session \n",
    "spark = SparkSession \\\n",
    "    .builder \\\n",
    "    .appName(\"Wrangling Data\") \\\n",
    "    .getOrCreate()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "path = \"data/sparkify_log_small.json\"\n",
    "user_log = spark.read.json(path)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Data Exploration \n",
    "\n",
    "The next cells explore the data set."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[Row(artist='Showaddywaddy', auth='Logged In', firstName='Kenneth', gender='M', itemInSession=112, lastName='Matthews', length=232.93342, level='paid', location='Charlotte-Concord-Gastonia, NC-SC', method='PUT', page='NextSong', registration=1509380319284, sessionId=5132, song='Christmas Tears Will Fall', status=200, ts=1513720872284, userAgent='\"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36\"', userId='1046'),\n",
       " Row(artist='Lily Allen', auth='Logged In', firstName='Elizabeth', gender='F', itemInSession=7, lastName='Chase', length=195.23873, level='free', location='Shreveport-Bossier City, LA', method='PUT', page='NextSong', registration=1512718541284, sessionId=5027, song='Cheryl Tweedy', status=200, ts=1513720878284, userAgent='\"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.143 Safari/537.36\"', userId='1000'),\n",
       " Row(artist='Cobra Starship Featuring Leighton Meester', auth='Logged In', firstName='Vera', gender='F', itemInSession=6, lastName='Blackwell', length=196.20526, level='paid', location='Racine, WI', method='PUT', page='NextSong', registration=1499855749284, sessionId=5516, song='Good Girls Go Bad (Feat.Leighton Meester) (Album Version)', status=200, ts=1513720881284, userAgent='\"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.78.2 (KHTML, like Gecko) Version/7.0.6 Safari/537.78.2\"', userId='2219'),\n",
       " Row(artist='Alex Smoke', auth='Logged In', firstName='Sophee', gender='F', itemInSession=8, lastName='Barker', length=405.99465, level='paid', location='San Luis Obispo-Paso Robles-Arroyo Grande, CA', method='PUT', page='NextSong', registration=1513009647284, sessionId=2372, song=\"Don't See The Point\", status=200, ts=1513720905284, userAgent='\"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.143 Safari/537.36\"', userId='2373'),\n",
       " Row(artist=None, auth='Logged In', firstName='Jordyn', gender='F', itemInSession=0, lastName='Jones', length=None, level='free', location='Syracuse, NY', method='GET', page='Home', registration=1513648531284, sessionId=1746, song=None, status=200, ts=1513720913284, userAgent='\"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.94 Safari/537.36\"', userId='1747')]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_log.take(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "root\n",
      " |-- artist: string (nullable = true)\n",
      " |-- auth: string (nullable = true)\n",
      " |-- firstName: string (nullable = true)\n",
      " |-- gender: string (nullable = true)\n",
      " |-- itemInSession: long (nullable = true)\n",
      " |-- lastName: string (nullable = true)\n",
      " |-- length: double (nullable = true)\n",
      " |-- level: string (nullable = true)\n",
      " |-- location: string (nullable = true)\n",
      " |-- method: string (nullable = true)\n",
      " |-- page: string (nullable = true)\n",
      " |-- registration: long (nullable = true)\n",
      " |-- sessionId: long (nullable = true)\n",
      " |-- song: string (nullable = true)\n",
      " |-- status: long (nullable = true)\n",
      " |-- ts: long (nullable = true)\n",
      " |-- userAgent: string (nullable = true)\n",
      " |-- userId: string (nullable = true)\n",
      "\n"
     ]
    }
   ],
   "source": [
    "user_log.printSchema()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "+-------+-----------------+----------+---------+------+------------------+--------+-----------------+-----+------------+------+-------+--------------------+------------------+--------+------------------+--------------------+--------------------+------------------+\n",
      "|summary|           artist|      auth|firstName|gender|     itemInSession|lastName|           length|level|    location|method|   page|        registration|         sessionId|    song|            status|                  ts|           userAgent|            userId|\n",
      "+-------+-----------------+----------+---------+------+------------------+--------+-----------------+-----+------------+------+-------+--------------------+------------------+--------+------------------+--------------------+--------------------+------------------+\n",
      "|  count|             8347|     10000|     9664|  9664|             10000|    9664|             8347|10000|        9664| 10000|  10000|                9664|             10000|    8347|             10000|               10000|                9664|             10000|\n",
      "|   mean|            461.0|      null|     null|  null|           19.6734|    null|249.6486587492503| null|        null|  null|   null|1.504695369588739...|         4436.7511|Infinity|          202.8984|  1.5137859954164E12|                null|1442.4413286423842|\n",
      "| stddev|            300.0|      null|     null|  null|25.382114916132597|    null|95.00437130781461| null|        null|  null|   null|  8.47314252131657E9|2043.1281541827557|     NaN|18.041791154505876|3.2908288623601213E7|                null| 829.8909432082613|\n",
      "|    min|              !!!|     Guest|   Aakash|     F|                 0| Acevedo|          1.12281| free|Aberdeen, WA|   GET|  About|       1463503881284|                 9|      #1|               200|       1513720872284|\"Mozilla/5.0 (Mac...|                  |\n",
      "|    max|ÃÂlafur Arnalds|Logged Out|     Zoie|     M|               163|  Zuniga|        1806.8371| paid|    Yuma, AZ|   PUT|Upgrade|       1513760702284|              7144|wingless|               404|       1513848349284|Mozilla/5.0 (comp...|               999|\n",
      "+-------+-----------------+----------+---------+------+------------------+--------+-----------------+-----+------------+------+-------+--------------------+------------------+--------+------------------+--------------------+--------------------+------------------+\n",
      "\n"
     ]
    }
   ],
   "source": [
    "user_log.describe().show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "+-------+-----------------+\n",
      "|summary|           artist|\n",
      "+-------+-----------------+\n",
      "|  count|             8347|\n",
      "|   mean|            461.0|\n",
      "| stddev|            300.0|\n",
      "|    min|              !!!|\n",
      "|    max|ÃÂlafur Arnalds|\n",
      "+-------+-----------------+\n",
      "\n"
     ]
    }
   ],
   "source": [
    "user_log.describe(\"artist\").show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "+-------+------------------+\n",
      "|summary|         sessionId|\n",
      "+-------+------------------+\n",
      "|  count|             10000|\n",
      "|   mean|         4436.7511|\n",
      "| stddev|2043.1281541827557|\n",
      "|    min|                 9|\n",
      "|    max|              7144|\n",
      "+-------+------------------+\n",
      "\n"
     ]
    }
   ],
   "source": [
    "user_log.describe(\"sessionId\").show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10000"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_log.count()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "+----------------+\n",
      "|            page|\n",
      "+----------------+\n",
      "|           About|\n",
      "|       Downgrade|\n",
      "|           Error|\n",
      "|            Help|\n",
      "|            Home|\n",
      "|           Login|\n",
      "|          Logout|\n",
      "|        NextSong|\n",
      "|   Save Settings|\n",
      "|        Settings|\n",
      "|Submit Downgrade|\n",
      "|  Submit Upgrade|\n",
      "|         Upgrade|\n",
      "+----------------+\n",
      "\n"
     ]
    }
   ],
   "source": [
    "user_log.select(\"page\").dropDuplicates().sort(\"page\").show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[Row(userId='1046', firstname='Kenneth', page='NextSong', song='Christmas Tears Will Fall'),\n",
       " Row(userId='1046', firstname='Kenneth', page='NextSong', song='Be Wary Of A Woman'),\n",
       " Row(userId='1046', firstname='Kenneth', page='NextSong', song='Public Enemy No.1'),\n",
       " Row(userId='1046', firstname='Kenneth', page='NextSong', song='Reign Of The Tyrants'),\n",
       " Row(userId='1046', firstname='Kenneth', page='NextSong', song='Father And Son'),\n",
       " Row(userId='1046', firstname='Kenneth', page='NextSong', song='No. 5'),\n",
       " Row(userId='1046', firstname='Kenneth', page='NextSong', song='Seventeen'),\n",
       " Row(userId='1046', firstname='Kenneth', page='Home', song=None),\n",
       " Row(userId='1046', firstname='Kenneth', page='NextSong', song='War on war'),\n",
       " Row(userId='1046', firstname='Kenneth', page='NextSong', song='Killermont Street'),\n",
       " Row(userId='1046', firstname='Kenneth', page='NextSong', song='Black & Blue'),\n",
       " Row(userId='1046', firstname='Kenneth', page='Logout', song=None),\n",
       " Row(userId='1046', firstname='Kenneth', page='Home', song=None),\n",
       " Row(userId='1046', firstname='Kenneth', page='NextSong', song='Heads Will Roll'),\n",
       " Row(userId='1046', firstname='Kenneth', page='NextSong', song='Bleed It Out [Live At Milton Keynes]'),\n",
       " Row(userId='1046', firstname='Kenneth', page='NextSong', song='Clocks'),\n",
       " Row(userId='1046', firstname='Kenneth', page='NextSong', song='Love Rain'),\n",
       " Row(userId='1046', firstname='Kenneth', page='NextSong', song=\"Ry Ry's Song (Album Version)\"),\n",
       " Row(userId='1046', firstname='Kenneth', page='NextSong', song='The Invisible Man'),\n",
       " Row(userId='1046', firstname='Kenneth', page='NextSong', song='Catch You Baby (Steve Pitron & Max Sanna Radio Edit)'),\n",
       " Row(userId='1046', firstname='Kenneth', page='NextSong', song='Ask The Mountains'),\n",
       " Row(userId='1046', firstname='Kenneth', page='NextSong', song='Given Up (Album Version)'),\n",
       " Row(userId='1046', firstname='Kenneth', page='NextSong', song='El Cuatrero'),\n",
       " Row(userId='1046', firstname='Kenneth', page='NextSong', song='Hero/Heroine'),\n",
       " Row(userId='1046', firstname='Kenneth', page='NextSong', song='Spring'),\n",
       " Row(userId='1046', firstname='Kenneth', page='NextSong', song='Rising Moon'),\n",
       " Row(userId='1046', firstname='Kenneth', page='NextSong', song='Tough Little Boys'),\n",
       " Row(userId='1046', firstname='Kenneth', page='NextSong', song=\"Qu'Est-Ce Que T'Es Belle\"),\n",
       " Row(userId='1046', firstname='Kenneth', page='NextSong', song='Secrets'),\n",
       " Row(userId='1046', firstname='Kenneth', page='NextSong', song='Under The Gun')]"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_log.select([\"userId\", \"firstname\", \"page\", \"song\"]).where(user_log.userId == \"1046\").collect()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Calculating Statistics by Hour"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "get_hour = udf(lambda x: datetime.datetime.fromtimestamp(x / 1000.0). hour)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "user_log = user_log.withColumn(\"hour\", get_hour(user_log.ts))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Row(artist='Showaddywaddy', auth='Logged In', firstName='Kenneth', gender='M', itemInSession=112, lastName='Matthews', length=232.93342, level='paid', location='Charlotte-Concord-Gastonia, NC-SC', method='PUT', page='NextSong', registration=1509380319284, sessionId=5132, song='Christmas Tears Will Fall', status=200, ts=1513720872284, userAgent='\"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36\"', userId='1046', hour='22')"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_log.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "songs_in_hour = user_log.filter(user_log.page == \"NextSong\").groupby(user_log.hour).count().orderBy(user_log.hour.cast(\"float\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "+----+-----+\n",
      "|hour|count|\n",
      "+----+-----+\n",
      "|   0|  456|\n",
      "|   1|  454|\n",
      "|   2|  382|\n",
      "|   3|  302|\n",
      "|   4|  352|\n",
      "|   5|  276|\n",
      "|   6|  348|\n",
      "|   7|  358|\n",
      "|   8|  375|\n",
      "|   9|  249|\n",
      "|  10|  216|\n",
      "|  11|  228|\n",
      "|  12|  251|\n",
      "|  13|  339|\n",
      "|  14|  462|\n",
      "|  15|  479|\n",
      "|  16|  484|\n",
      "|  17|  430|\n",
      "|  18|  362|\n",
      "|  19|  295|\n",
      "+----+-----+\n",
      "only showing top 20 rows\n",
      "\n"
     ]
    }
   ],
   "source": [
    "songs_in_hour.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "songs_in_hour_pd = songs_in_hour.toPandas()\n",
    "songs_in_hour_pd.hour = pd.to_numeric(songs_in_hour_pd.hour)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYgAAAEKCAYAAAAIO8L1AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4wLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvpW3flQAAFo1JREFUeJzt3X+w5XV93/HnKwvoNhpXYKGwsCzGldGWCrhSWjIGNRMEO4UyUjWJbgzTjZUkpCqKGTPatIlYp8EaK3ETqIv1R6nyaxKiMAtEncYfiyDgIGVDKeyPYSH8CFSi/Hj3j/O9udfle+9+9+w9P+45z8fMnfP9fs73nPu5Z849r/P5fD+fzzdVhSRJu/upUVdAkjSeDAhJUisDQpLUyoCQJLUyICRJrQwISVIrA0KS1MqAkCS1MiAkSa32G3UF9sXBBx9ca9asGXU1JGlJufnmmx+qqpV7Om5JB8SaNWvYsmXLqKshSUtKkv/b5Ti7mCRJrQwISVIrA0KS1MqAkCS1MiAkSa0MCElSKwNCktTKgJAktTIgJEmtDAhJUisDQpLUyoCQJLUyICRJrQwISVIrA0KS1MqAkCS1MiAkSa0MCElSKwNCktTKgJAktTIgJEmtDAhJUisDQpLUyoCQJLUyICRJrQYaEEnuTXJ7kluTbGnKDkxyfZK7m9sXN+VJ8okkW5PcluSEQdZNkrSwYbQgXltVx1XVumb/AmBzVa0FNjf7AKcBa5ufDcDFQ6ibJGkeo+hiOgPY1GxvAs6cU35Z9XwTWJHksBHUT5LE4AOigOuS3JxkQ1N2aFXtBGhuD2nKVwH3z3nstqZMkjQC+w34+U+uqh1JDgGuT/KDBY5NS1k956Be0GwAWL169eLUUpL0HANtQ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Abzb956fptUjyBeAUeit1PgB8CLgKuBxYDdwHnF1VE3/ydp7X4hR63UsF3Av8+kwf/CRL8nPA14HbgWeb4t+hdx5i6t4bMGUBIUnqbpq6mCRJe8GAkCS1MiAkSa0MCElSKwNCktTKgJA6SvLEbvu/muSTo6qPNGgGhDRizUrD0tgxIKRFkOSoJJubBe42J1ndlH8myZvmHPdEc3tKc+2Bz9ObmCWNnbG6JrU05pYnuXXO/oHMLtfySeCyqtqU5NeAT7DnZaFPBP5xVf2fxa+qtO8MCKm7J6vquJmdJL8KzFxs6Z8BZzXbnwX+U4fn+7bhoHFmF5M0GDNr2DxN83/WLAZ3wJxj/t+wKyXtDQNCWhz/i94KwQC/DHyj2b4XeFWzfQaw/3CrJfXPgJAWx28B70hyG/A24Lym/E+An0/ybeCfYqtBS4iruUqSWtmCkCS1MiAkSa0MCElSKwNCktTKgJAktTIgJEmtDAhJUisDQpLU6v8DkcbekFDqJEAAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7f40d0bfac18>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(songs_in_hour_pd[\"hour\"], songs_in_hour_pd[\"count\"])\n",
    "plt.xlim(-1, 24);\n",
    "plt.ylim(0, 1.2 * max(songs_in_hour_pd[\"count\"]))\n",
    "plt.xlabel(\"Hour\")\n",
    "plt.ylabel(\"Songs played\");"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Drop Rows with Missing Values\n",
    "\n",
    "As you'll see, it turns out there are no missing values in the userID or session columns. But there are userID values that are empty strings."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "user_log_valid = user_log.dropna(how = \"any\", subset = [\"userId\", \"sessionId\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10000"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_log_valid.count()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "+------+\n",
      "|userId|\n",
      "+------+\n",
      "|      |\n",
      "|    10|\n",
      "|   100|\n",
      "|  1000|\n",
      "|  1003|\n",
      "|  1005|\n",
      "|  1006|\n",
      "|  1017|\n",
      "|  1019|\n",
      "|  1020|\n",
      "|  1022|\n",
      "|  1025|\n",
      "|  1030|\n",
      "|  1035|\n",
      "|  1037|\n",
      "|   104|\n",
      "|  1040|\n",
      "|  1042|\n",
      "|  1043|\n",
      "|  1046|\n",
      "+------+\n",
      "only showing top 20 rows\n",
      "\n"
     ]
    }
   ],
   "source": [
    "user_log.select(\"userId\").dropDuplicates().sort(\"userId\").show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "user_log_valid = user_log_valid.filter(user_log_valid[\"userId\"] != \"\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "9664"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_log_valid.count()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Users Downgrade Their Accounts\n",
    "\n",
    "Find when users downgrade their accounts and then flag those log entries. Then use a window function and cumulative sum to distinguish each user's data as either pre or post downgrade events."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "+------+---------+---------+------+-------------+--------+------+-----+--------------------+------+----------------+-------------+---------+----+------+-------------+--------------------+------+----+\n",
      "|artist|     auth|firstName|gender|itemInSession|lastName|length|level|            location|method|            page| registration|sessionId|song|status|           ts|           userAgent|userId|hour|\n",
      "+------+---------+---------+------+-------------+--------+------+-----+--------------------+------+----------------+-------------+---------+----+------+-------------+--------------------+------+----+\n",
      "|  null|Logged In|    Kelly|     F|           24|  Newton|  null| paid|Houston-The Woodl...|   PUT|Submit Downgrade|1513283366284|     5931|null|   307|1513768454284|Mozilla/5.0 (Wind...|  1138|  11|\n",
      "+------+---------+---------+------+-------------+--------+------+-----+--------------------+------+----------------+-------------+---------+----+------+-------------+--------------------+------+----+\n",
      "\n"
     ]
    }
   ],
   "source": [
    "user_log_valid.filter(\"page = 'Submit Downgrade'\").show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[Row(userId='1138', firstname='Kelly', page='Home', level='paid', song=None),\n",
       " Row(userId='1138', firstname='Kelly', page='NextSong', level='paid', song='Everybody Everybody'),\n",
       " Row(userId='1138', firstname='Kelly', page='NextSong', level='paid', song='Gears'),\n",
       " Row(userId='1138', firstname='Kelly', page='NextSong', level='paid', song='Use Somebody'),\n",
       " Row(userId='1138', firstname='Kelly', page='NextSong', level='paid', song='Love Of My Life (1993 Digital Remaster)'),\n",
       " Row(userId='1138', firstname='Kelly', page='NextSong', level='paid', song='Down In The Valley Woe'),\n",
       " Row(userId='1138', firstname='Kelly', page='NextSong', level='paid', song='Treat Her Like A Lady'),\n",
       " Row(userId='1138', firstname='Kelly', page='NextSong', level='paid', song=\"Everybody Thinks You're An Angel\"),\n",
       " Row(userId='1138', firstname='Kelly', page='NextSong', level='paid', song='Fourteen Wives'),\n",
       " Row(userId='1138', firstname='Kelly', page='NextSong', level='paid', song='Love On The Rocks'),\n",
       " Row(userId='1138', firstname='Kelly', page='NextSong', level='paid', song='Breakeven'),\n",
       " Row(userId='1138', firstname='Kelly', page='NextSong', level='paid', song='Leaf House'),\n",
       " Row(userId='1138', firstname='Kelly', page='NextSong', level='paid', song='NAISEN KANSSA'),\n",
       " Row(userId='1138', firstname='Kelly', page='NextSong', level='paid', song=\"You're In My Heart\"),\n",
       " Row(userId='1138', firstname='Kelly', page='NextSong', level='paid', song='Roll On Down The Highway'),\n",
       " Row(userId='1138', firstname='Kelly', page='NextSong', level='paid', song='Plasticities (Remix)'),\n",
       " Row(userId='1138', firstname='Kelly', page='NextSong', level='paid', song='Secrets'),\n",
       " Row(userId='1138', firstname='Kelly', page='NextSong', level='paid', song='Hello'),\n",
       " Row(userId='1138', firstname='Kelly', page='NextSong', level='paid', song='I Never Told You'),\n",
       " Row(userId='1138', firstname='Kelly', page='NextSong', level='paid', song='Love Break Me'),\n",
       " Row(userId='1138', firstname='Kelly', page='NextSong', level='paid', song='One Touch One Bounce'),\n",
       " Row(userId='1138', firstname='Kelly', page='NextSong', level='paid', song='Undo'),\n",
       " Row(userId='1138', firstname='Kelly', page='NextSong', level='paid', song='Overdue (Blackbeard Remix)'),\n",
       " Row(userId='1138', firstname='Kelly', page='NextSong', level='paid', song='Slave To Love (1999 Digital Remaster)'),\n",
       " Row(userId='1138', firstname='Kelly', page='NextSong', level='paid', song='Stronger'),\n",
       " Row(userId='1138', firstname='Kelly', page='NextSong', level='paid', song='All Of Us (Album Version)'),\n",
       " Row(userId='1138', firstname='Kelly', page='NextSong', level='paid', song='Sehr kosmisch'),\n",
       " Row(userId='1138', firstname='Kelly', page='NextSong', level='paid', song='March Of The Celts'),\n",
       " Row(userId='1138', firstname='Kelly', page='NextSong', level='paid', song='Electricity'),\n",
       " Row(userId='1138', firstname='Kelly', page='NextSong', level='paid', song='Aces High'),\n",
       " Row(userId='1138', firstname='Kelly', page='NextSong', level='paid', song='Bananeira'),\n",
       " Row(userId='1138', firstname='Kelly', page='NextSong', level='paid', song='The General'),\n",
       " Row(userId='1138', firstname='Kelly', page='NextSong', level='paid', song='HÃ\\x83Â©roe De Leyenda (VersiÃ\\x83Â³n Maxi)'),\n",
       " Row(userId='1138', firstname='Kelly', page='NextSong', level='paid', song=\"Don't Stop The Music\"),\n",
       " Row(userId='1138', firstname='Kelly', page='NextSong', level='paid', song=\"You're The One\"),\n",
       " Row(userId='1138', firstname='Kelly', page='NextSong', level='paid', song='Entering White Cecilia'),\n",
       " Row(userId='1138', firstname='Kelly', page='NextSong', level='paid', song='Piccolo Cesare'),\n",
       " Row(userId='1138', firstname='Kelly', page='Help', level='paid', song=None),\n",
       " Row(userId='1138', firstname='Kelly', page='NextSong', level='paid', song='Last Christmas (Album Version)'),\n",
       " Row(userId='1138', firstname='Kelly', page='NextSong', level='paid', song='You Shook Me'),\n",
       " Row(userId='1138', firstname='Kelly', page='NextSong', level='paid', song='Going Steady'),\n",
       " Row(userId='1138', firstname='Kelly', page='NextSong', level='paid', song='My Name Is'),\n",
       " Row(userId='1138', firstname='Kelly', page='NextSong', level='paid', song='Undo'),\n",
       " Row(userId='1138', firstname='Kelly', page='NextSong', level='paid', song='Secrets'),\n",
       " Row(userId='1138', firstname='Kelly', page='NextSong', level='paid', song='Good Times Gone (Album Version)'),\n",
       " Row(userId='1138', firstname='Kelly', page='NextSong', level='paid', song='Angelito'),\n",
       " Row(userId='1138', firstname='Kelly', page='NextSong', level='paid', song='Batdance ( LP Version )'),\n",
       " Row(userId='1138', firstname='Kelly', page='Home', level='paid', song=None),\n",
       " Row(userId='1138', firstname='Kelly', page='NextSong', level='paid', song='DiÃ\\x83Â¡kdal'),\n",
       " Row(userId='1138', firstname='Kelly', page='NextSong', level='paid', song='Whirring'),\n",
       " Row(userId='1138', firstname='Kelly', page='NextSong', level='paid', song='Potholderz (feat. Count Bass D)'),\n",
       " Row(userId='1138', firstname='Kelly', page='NextSong', level='paid', song='Seaside'),\n",
       " Row(userId='1138', firstname='Kelly', page='NextSong', level='paid', song='Louder Than A Bomb'),\n",
       " Row(userId='1138', firstname='Kelly', page='NextSong', level='paid', song='Just Like You'),\n",
       " Row(userId='1138', firstname='Kelly', page='NextSong', level='paid', song=\"You're The One\"),\n",
       " Row(userId='1138', firstname='Kelly', page='NextSong', level='paid', song='Turn It Again (Album Version)'),\n",
       " Row(userId='1138', firstname='Kelly', page='NextSong', level='paid', song='Everywhere I Go'),\n",
       " Row(userId='1138', firstname='Kelly', page='NextSong', level='paid', song=\"Easy Skankin'\"),\n",
       " Row(userId='1138', firstname='Kelly', page='NextSong', level='paid', song='Roses'),\n",
       " Row(userId='1138', firstname='Kelly', page='NextSong', level='paid', song='Killing Me Softly With His Song'),\n",
       " Row(userId='1138', firstname='Kelly', page='NextSong', level='paid', song='The Razor (Album Version)'),\n",
       " Row(userId='1138', firstname='Kelly', page='NextSong', level='paid', song='Idols and Anchors'),\n",
       " Row(userId='1138', firstname='Kelly', page='Downgrade', level='paid', song=None),\n",
       " Row(userId='1138', firstname='Kelly', page='Submit Downgrade', level='paid', song=None),\n",
       " Row(userId='1138', firstname='Kelly', page='Home', level='free', song=None),\n",
       " Row(userId='1138', firstname='Kelly', page='NextSong', level='free', song='Bones'),\n",
       " Row(userId='1138', firstname='Kelly', page='Home', level='free', song=None),\n",
       " Row(userId='1138', firstname='Kelly', page='NextSong', level='free', song='Grenouilles Mantidactylus (Small Frogs)')]"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_log.select([\"userId\", \"firstname\", \"page\", \"level\", \"song\"]).where(user_log.userId == \"1138\").collect()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "flag_downgrade_event = udf(lambda x: 1 if x == \"Submit Downgrade\" else 0, IntegerType())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "user_log_valid = user_log_valid.withColumn(\"downgraded\", flag_downgrade_event(\"page\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Row(artist='Showaddywaddy', auth='Logged In', firstName='Kenneth', gender='M', itemInSession=112, lastName='Matthews', length=232.93342, level='paid', location='Charlotte-Concord-Gastonia, NC-SC', method='PUT', page='NextSong', registration=1509380319284, sessionId=5132, song='Christmas Tears Will Fall', status=200, ts=1513720872284, userAgent='\"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36\"', userId='1046', hour='22', downgraded=0)"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_log_valid.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyspark.sql import Window"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "windowval = Window.partitionBy(\"userId\").orderBy(desc(\"ts\")).rangeBetween(Window.unboundedPreceding, 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "user_log_valid = user_log_valid.withColumn(\"phase\", Fsum(\"downgraded\").over(windowval))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "user_log_valid.select([\"userId\", \"firstname\", \"ts\", \"page\", \"level\", \"phase\",'downgraded']).where(user_log.userId == \"1138\").sort(\"ts\").collect()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
